//
//  OrderDetailViewController.m
//  SXH
//
//  Created by Ryan on 16/4/8.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "OrderDetailCell1.h"
#import "OrderDetailCell2.h"
#import "CommonService.h"
#import "OrderDetailModel.h"
#import "SpecialTouristSection.h"
#import "TouristinfoCell.h"
#import "OrderDetailCell3.h"
@interface OrderDetailViewController ()<SpecialTouristSectionDelegate>
@property (nonatomic , strong) SpecialTouristSection *specialTouristSection;
@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self getDetailInfo];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0) {
            if ([self.detailModel.product_class_id isEqualToString:@"1"]) {
                return 175;
            }
            else
            {
                return 210;
            }
        }
        else if (indexPath.row == 1)
        {
            return 145;
        }
        else if (indexPath.row == 2)
        {
            return 217;
        }
    }
    else
    {
        return 66;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        if ([self.detailModel.product_class_id isEqualToString:@"1"]) {
            return 2;
        }
        else
        {
            return 3;
        }
    }
    else
    {
        if (self.specialTouristSection.isExpand)
    {
        return [self.detailModel.tourism_data.people_list count];
    }
        else
    {
        return 0;
    }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 44;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0)
        {
            NSString *cellId = @"OrderDetailCell1";
            OrderDetailCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
            }
            if ([self.detailModel.product_class_id isEqualToString:@"1"])
            {
                cell.vertifyLabel.hidden = YES;
                cell.checkNumLabel.text = self.detailModel.check_num;
            }
            else
            {
                cell.vertifyLabel.hidden = NO;
                
            }
            cell.oderNumLabel.text = self.detailModel.order_num;
            cell.orderTime.text = self.detailModel.order_time;
            NSMutableString * totalNumLabelstring = [[NSMutableString alloc]init];
            
            if ([self.detailModel.tourism_data.adult_num intValue] > 0)
            {
                [totalNumLabelstring appendString:[NSString stringWithFormat:@"%zi 成人",[self.detailModel.tourism_data.adult_num intValue]]];
                [totalNumLabelstring appendString:@"  "];
            }
            if ([self.detailModel.tourism_data.aged_num intValue] > 0)
            {
                [totalNumLabelstring appendString:[NSString stringWithFormat:@"%zi 老人",[self.detailModel.tourism_data.aged_num intValue]]];
                [totalNumLabelstring appendString:@"  "];
            }
            if ([self.detailModel.tourism_data.child_a_num intValue] > 0)
            {
                [totalNumLabelstring appendString:[NSString stringWithFormat:@"%zi 儿童(无座)",[self.detailModel.tourism_data.adult_num intValue]]];
                [totalNumLabelstring appendString:@"  "];
            }
            if ([self.detailModel.tourism_data.child_b_num intValue] > 0)
            {
                [totalNumLabelstring appendString:[NSString stringWithFormat:@"%zi 儿童",[self.detailModel.tourism_data.child_b_num intValue]]];
                [totalNumLabelstring appendString:@"  "];
            }
            if ([self.detailModel.tourism_data.baby_num intValue] > 0)
            {
                [totalNumLabelstring appendString:[NSString stringWithFormat:@"%zi 婴儿",[self.detailModel.tourism_data.baby_num intValue]]];
                [totalNumLabelstring appendString:@"  "];
            }
            cell.totalNumLabel.text = self.detailModel.qty;;
            cell.totalAmountLabel.text = [NSString stringWithFormat:@"¥ %@",self.detailModel.real_total_amount];
            if ([self.detailModel.status isEqualToString:@"101"])
            {
                cell.statusImageView.image = [UIImage imageNamed:@"未付款"];
            }
            if ([self.detailModel.status isEqualToString:@"102"])
            {
                cell.statusImageView.image = [UIImage imageNamed:@"已付款"];
            }
            else if([self.detailModel.status isEqualToString:@"104"])
            {
                if ([self.order_entity.can_comment isEqualToString:@"2"]) {
                    cell.statusImageView.image = [UIImage imageNamed:@"待评价"];
                }
                else
                {
                    cell.statusImageView.image = [UIImage imageNamed:@"完成"];
                }
            }
            else if([self.detailModel.status isEqualToString:@"106"])
            {
                cell.statusImageView.image = [UIImage imageNamed:@"退单中"];
            }
            else if([self.detailModel.status isEqualToString:@"107"])
            {
                cell.statusImageView.image = [UIImage imageNamed:@"退单成功"];
            }
            return cell;
            return cell;
        }
        else if (indexPath.row == 1)
        {
            NSString *cellId = @"OrderDetailCell2";
            OrderDetailCell2 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
            }
            cell.productNameLabel.text = self.detailModel.product_name;
            cell.travelTimeLabel.text = self.detailModel.tourism_data.departure_date;
            return cell;
        }
        else if (indexPath.row == 2)
        {
            NSString *cellId = @"OrderDetailCell3";
            OrderDetailCell3 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
            }
            cell.qrString = [NSString stringWithFormat:@"%@_%@",_order_id,self.detailModel.check_num];
//            cell.productNameLabel.text = self.detailModel.product_name;
//            cell.travelTimeLabel.text = self.detailModel.tourism_data.departure_date;
            return cell;
        }

    }
    else
    {
        NSString *cellId = @"TouristinfoCell";
        TouristinfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil)
        {
            cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
        }
        NSDictionary *dic = [self.detailModel.tourism_data.people_list objectAtIndex:indexPath.row];
        cell.nameLabel.text = [dic objectForKey:@"people_name"];
        cell.idNumLabel.text = [dic objectForKey:@"document_num"];
        cell.deleteBtn.hidden = YES;
        return cell;
    }
        return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        if(section == 1)
        {
            if (!self.specialTouristSection)
            {
                self.specialTouristSection = [[[NSBundle mainBundle]loadNibNamed:@"SpecialTouristSection" owner:self options:nil]lastObject];
                self.specialTouristSection.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
                self.specialTouristSection.delegate = self;
            }
    
            UIView *view = [[UIView alloc] initWithFrame:[self.specialTouristSection frame]];
            [view addSubview:self.specialTouristSection];
            return view;
        }
    return nil;
}




- (void)getDetailInfo
{
    [self showLoadingView:YES];
    CommonService *service = [[CommonService alloc]init];
    NSDictionary * parm = @{@"order_id":_order_id,
                            @"user_id":USERID};
    [service getNetWorkData:parm Path:USER_QUERY_BY_ID_NEW Successed:^(id entity) {
        if ([[NSString stringWithFormat:@"%@",[entity objectForKey:@"code"]]isEqualToString:@"0"])
        {
            self.detailModel = [[OrderDetailModel alloc]initWithDictionary:[entity objectForKey:@"data"]  error:nil];
            [self.view addSubview:self.tableView];
            [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(62, 0, 0, 0));
            }];
        }
//        self.detailModel = [[OrderDetailModel alloc]initWithDictionary:[entity objectForKey:@"data"]  error:nil];
//        [self.view addSubview:self.tableView];
//        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(62, 0, 0, 0));
//        }];
        [self showLoadingView:NO];
    } Failed:^(NSInteger errorCode, NSString *message) {
        [self showLoadingView:NO];

    }];
}


- (UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource =self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)sectiondidSelect:(BOOL)isExpand
{
    [self.tableView beginUpdates];
    NSMutableArray *indexPathArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.detailModel.tourism_data.people_list count] ; i++)
    {
        [indexPathArray addObject:[NSIndexPath indexPathForRow:i inSection:1]];
    }
    if (!isExpand)
    {
        [self.tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        [self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    [self.tableView endUpdates];
}

- (IBAction)doback:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
